<template>
  <div class="programmeForm">
    <!-- element组件库的Dialog对话框默认可以通过点击 modal 关闭 Dialog，即点击空白处弹框可关闭。 -->
    <!-- :visible.sync 子组键改变父组件的值 控制子组件的显示-->
    <el-dialog title="新增方案" :visible.sync="editDialogIsShow" :before-close="handleClose" :close-on-click-modal="false">
      <el-form :model="programme" label-width="80px" ref="editProgrammeForm">
        <el-form-item label="专家">
          <el-col :span="4">
            <el-select v-model="programme.eno" placeholder="请选择">
              <el-option v-for="(programmegroup,index) in allProgrammeGroup" :key="programmegroup.num" :label="programmegroup.programmeName" :value="programmegroup.programmeName"/>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="选择时间" prop="prdate">
          <el-col :span="4">
            <div class="block">
              <el-date-picker
                v-model="programme.prdate"
                type="datetime"
                placeholder="选择日期时间"
                default-time="00:00:00">
              </el-date-picker>
            </div>
          </el-col>
        </el-form-item>
        <el-form-item label="金额" prop="prprice">
          <el-col :span="6">
            <el-input v-model="programme.prprice" placeholder="请填入金额" @input="updateView($event)" clearable />
          </el-col>
        </el-form-item>
        <el-form-item label="标题" prop="prtitle">
          <el-col :span="12">
            <el-input
              type="text"
              :rows="11"
              placeholder="请输入标题"
              v-model="programme.prtitle"
              maxlength="10"
              show-word-limit
            >
            </el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="内容" prop="prcontent">
          <el-col :span="12">
            <div style="margin: 20px 0;"></div>
            <el-input
              type="textarea"
              :rows="11"
              placeholder="请输入内容"
              v-model="programme.prcontent"
              maxlength="100"
              show-word-limit
            >
            </el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <el-row>
        <el-col :span="12">
          <span>&nbsp;</span></el-col>
        <el-col :span="6">
          <span><el-button type="primary" @click="editProgramme('editProgrammeForm')">增加</el-button></span>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
  import api from "../../../../axios/api"
  export default {
    name: "editProgramme",
    data(){
      return {
        programme:{eno:"",ernu:"",prtitle:"",prcontent:"",prdate:"",prprice:"",prbuynum:"",prlikes:""},
        allProgrammeGroup:[{num:0,"programmeName":"李专家"},{num:1,"programmeName":"赵专家"},{num:2,"programmeName":"王专家"},{num:3,"programmeName":"罗专家"},{num:4,"programmeName":"刘专家"}],
        editDialogIsShow:false,
      }
    },
    methods: {
      init(programme) {
        this.editDialogIsShow = true;
        this.programme = programme;
        console.log(programme);
      },
      editProgramme:function(editProgrammeForm){
        this.$refs[editProgrammeForm].validate(valid => {//此处refs对象和标签上的ref对应
          if (valid) {
            //如果通过验证 to do...
            console.log(this.programme);
            this.setDataFromAxios();
            this.handleClose();
          } else {
            console.log('error submit!!')
            return false
          }
        });
      },
      updateView(e) {
        this.$forceUpdate();
      },
      setDataFromAxios:function(){
        //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
        api.setDataFromAxios("/api/programme",this.programme,"patch")
          .then(res=>{
            this.$message.success(this.programme.ename+"增加成功")
            location.reload();
          });//.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
      },
      //关闭页面
      handleClose() {
        this.editDialogIsShow = false;
        //给父组件传参
        this.$emit("editDialogIsShow", this.editDialogIsShow);
      },
    },
    //监听
    watch: {
      editDialogIsShow(newValue, oldValue) {//配合:visible.sync监听变化
        this.editDialogIsShow = newValue;
      }
    }
  }
</script>
<style scoped lang="stylus">
  .programmeForm
    margin 0 auto
    width 1000px
    text-align left

    .el-form
      text-align:center;
</style>
